package com.fadadianqi.logistics.admin.controller;

import com.fadadianqi.logistics.admin.controller.SellController.BillVO;
import com.fadadianqi.logistics.admin.controller.SellController.BillVO.BillVOItem;
import com.fadadianqi.commons.BackToURLUtils;
import com.fadadianqi.logistics.customer.model.Customer;
import com.fadadianqi.logistics.customer.model.Town;
import com.fadadianqi.logistics.customer.model.Village;
import com.fadadianqi.logistics.customer.service.CustomerService;
import com.fadadianqi.goods.model.Brand;
import com.fadadianqi.goods.model.Category;
import com.fadadianqi.goods.model.Goods;
import com.fadadianqi.goods.service.GoodsService;
import com.fadadianqi.logistics.sell.model.Bill;
import com.fadadianqi.logistics.sell.model.Item;
import com.fadadianqi.logistics.sell.service.BillService;
import com.yeqiangwei.commons.util.BeanUtils;
import com.yeqiangwei.commons.util.ParamUtils;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class SellReturnController {

    private static final String VIEW_FORM = "admin/sell/return";

    private static final String PARAM_BACK_URL = "backURL";

    @RequestMapping(value = URIMappingUtils.INVENTORY_PREFIX + "/sell/return"
            , method = RequestMethod.POST)
    public String doSave(HttpServletRequest request) {
        String[] ids = ParamUtils.getParameterValues(request, "item");
        Double returnMoney = ParamUtils.getDoubleParameter(request, "returnMoney", 0D);
        String toURL = ParamUtils.getString(request, PARAM_BACK_URL, "/a/sell/list");
        if (ValidatorHelper.isEmpty(ids)) {
            return "redirect:" + toURL;
        }
        billService.returnItems(ids, returnMoney);
        return "redirect:" + toURL;
    }

    @RequestMapping(value = URIMappingUtils.INVENTORY_PREFIX + "/sell/return"
            , method = RequestMethod.GET)
    public String showForm(HttpServletRequest request, ModelMap modelMap) {

        BackToURLUtils.setParamURLToAttr(request, PARAM_BACK_URL);

        String id = ParamUtils.getString(request, "id", null);
        Bill bill = billService.getById(id);
        List<Item> itemList = billService.findItemsByBillId(id);
        BillVO vo = new BillVO();
        vo.setId(bill.getId());
        List<BillVO.BillVOItem> voItemList = new ArrayList<BillVO.BillVOItem>();
        for (Item item : itemList) {
            BillVOItem voItem = new BillVOItem();

            voItem.setItem(item);

            voItem.setBillId(bill.getId());
            Goods goods = goodsService.getGoodsById(item.getGoodsId());
            voItem.setGoods(goods);

            Brand brand = goodsService.getBrandById(goods.getBrandId());
            voItem.setBrand(brand);

            Category category = goodsService.getCategoryById(goods.getCategoryId());
            voItem.setCategory(category);

            voItem.setPayables(item.getPayables());
            voItemList.add(voItem);
        }
        BeanUtils.copyProperties(vo, bill);
        Customer customer = customerService.getById(bill.getCustomerId());
        if (customer != null) {
            vo.setCustomer(customer);

            Town town = customerService.getTownById(customer.getTownId());
            vo.setTown(town);

            Village village = customerService.getVillageById(customer.getVillageId());
            vo.setVillage(village);
        }
        vo.setItemList(voItemList);
        modelMap.put("vo", vo);
        return VIEW_FORM;
    }

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private BillService billService;

    @Autowired
    private CustomerService customerService;
}
